package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.cloudbroker.bcs.common.util.DateTimeUtil;
import com.cloudbroker.bcs.common.util.StringUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.result.CCHistoryList;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.result.CCHistoryListData;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.result.CCRoomOperResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.vo.CCLiveListForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.chat.vo.CCRoomOperInfoForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.export.CCLiveBizService;
import com.cloudbroker.bcs.platform.dav.common.constant.SysContants;
import com.cloudbroker.bcs.platform.dav.middle.util.LiveUtil;

/**
 * Created by zhaojp18008 on 2016/9/8.
 */
@Service
public class CCLiveBizServiceImpl implements CCLiveBizService {
    
    private static final Logger logger = LoggerFactory.getLogger(CCLiveBizServiceImpl.class);
    
    public static final String LIVE_HISTORY_LIST = "http://api.csslcloud.net/api/live/info";
    
    @Override
    public CCRoomOperResult funcOperateCCRoom(CCRoomOperInfoForm form) {
        CCRoomOperResult ccRoomOperResult = new CCRoomOperResult();

        String request_url = form.getRequest_url();
        String request_params = form.getRequest_params();

        Map<String, String> params = (Map<String, String>) JSON.parse(request_params);
        if (params != null) {
            params.put("userid", "3ED5E098B40A25F3");
        }

        String cc_result = LiveUtil.callCCLiveApi(request_url, params, "D9y1iHTUuOnxWJ2Hr2C5IiL6Womwz8uz");
        ccRoomOperResult.setCc_result(cc_result);
        return VOUtil.setSuccessResult(ccRoomOperResult);
    }

    @Override
    public CCHistoryList funcGetCCListByID(CCLiveListForm param) {
        CCHistoryList result = new CCHistoryList();
        
        String room_id = param.getRoom_id();
        String page_num = param.getPage_num();
        String start_time = param.getStart_time();

        Map<String, String> params = new HashMap<String, String>();
        params.put("userid", "3ED5E098B40A25F3");
        params.put("roomid", room_id);
        params.put("pagenum", page_num);
        params.put("pageindex", "1");
        if(!StringUtil.isBlank(start_time)){
            params.put("endtime", URLEncoder.encode(start_time));
            //TODO 开始时间需完善
            params.put("starttime", URLEncoder.encode(SysContants.CC_START_TIME));
        }               
        String cc_result = LiveUtil.callCCLiveApi(LIVE_HISTORY_LIST, params, "D9y1iHTUuOnxWJ2Hr2C5IiL6Womwz8uz");
        Map<String, Object> res = (Map<String, Object>) JSON.parse(cc_result);
        if("OK".equals(res.get("result"))){
            result.setCount(res.get("count").toString());
            List<Map<String, Object>> list = (List<Map<String, Object>>) res.get("lives");
            List<CCHistoryListData> cclist = new ArrayList<CCHistoryListData>();
            if(null != list && list.size() > 0){
                for(int i=0,j=list.size();i<j;i++){
                    Map<String, Object> map = list.get(i);
                    if("0".equals(map.get("recordStatus")+"")){
                        continue;
                    }
                    CCHistoryListData data = new CCHistoryListData();
                    data.setId(map.get("id")+"");
                    data.setRecordStatus(map.get("recordStatus")+"");
                    data.setRecordVideoId(map.get("recordVideoId")+"");
                    data.setStartTime(map.get("startTime")+"");
                    data.setEndTime(map.get("endTime")+"");
                    data.setReplayUrl(map.get("replayUrl")+"");
                    cclist.add(data);
                }
                result.newDataList().addAll(cclist);
            }
        }else{
            logger.error("调CC失败");
        }
        return VOUtil.setSuccessResult(result);
    }
}
